#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>

// linux usage: g++ thread.cpp -lpthread -o thread
// linux编译必须带上lpthread，因为他不是linux下的默认库
pthread_t ntid;

void printids(const char *s){
    pid_t pid;
    pthread_t tid;
    
    pid = getpid();
    // 这里在mac下会编译失败，因为freeBSD的tid是个指针，linux下的tid才是无符号整形
    tid = pthread_self();
    
    printf("%s pid %u pid %u (0x%x)\n", s, (unsigned int)pid, (unsigned int)pid, (unsigned int)pid);
}

void* thr_fn(void *arg){
    printids("new thread: ");
    pthread_exit(NULL);
}

int main(){
    int err;
    
    err = pthread_create(&ntid, NULL, thr_fn, NULL);
    if(err != 0){
        printf("create thread error: %s\n", strerror(err));
        exit(0);
    }

    printids("main thread:");
    sleep(1);
    exit(0);
}